package com.gpms.interceptor;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.gpms.bean.Student;
import com.gpms.bean.SystemFile;
import com.gpms.bean.Topics;
import com.gpms.bean.UploadFile;
import com.gpms.service.Topics.IStudentTopicsOperate;
import com.gpms.service.Topics.TopicsOperateFactory;
import com.gpms.service.file.FileFactory;
import com.gpms.service.file.IFileService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class FileSubmitNumInterceptor extends AbstractInterceptor {

	private int fileClassNum;
	private int submitNum;
	private String title;
	private String content;
	public int getFileClassNum() {
		return fileClassNum;
	}
	public void setFileClassNum(int fileClassNum) {
		this.fileClassNum = fileClassNum;
	}
	public int getSubmitNum() {
		return submitNum;
	}
	public void setSubmitNum(int submitNum) {
		this.submitNum = submitNum;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext actionContext = ActionContext.getContext(); 
		Map session = actionContext.getSession(); 
		Object user=(Object)session.get("user");
		Student student = (Student)user;
		IStudentTopicsOperate operate = (IStudentTopicsOperate)TopicsOperateFactory.createOperate("student");
		Topics topics = operate.showMyTopics(student);
		IFileService fileOperate = FileFactory.createFileServer("file");
		List<SystemFile> listFile = fileOperate.showFileList(topics.getTopicsNum(), getFileClassNum());
		
		HttpServletRequest request = (HttpServletRequest)actionContext.get(ServletActionContext.HTTP_REQUEST);
		if(listFile==null)
		{
			return invocation.invoke();
		}
		UploadFile uf = (UploadFile)listFile.get(0);
		int num = uf.getSubmitNum();
		if(getSubmitNum()==0)
		{
			String type = (String)request.getAttribute("stage");
			if("1".equals(type))
			{
				if(num<1)
				{
					return invocation.invoke();
				}
			}else if("2".equals(type))
			{
				if(num<4)
				{
					return invocation.invoke();
				}
			}else if("3".equals(type))
			{
				if(num<7)
				{
					return invocation.invoke();
				}
			}
		}else if(num<getSubmitNum())
		{
			return invocation.invoke();
		}
		request.setAttribute("title", getTitle());
		request.setAttribute("content", getContent());
		return "input";
	}

}
